﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;
using Zen.Barcode;

namespace Worktool
{
    public partial class IpDevices : Form
    {
        private String pkInstalledPrinters;
        private PrintDocument label;        
        
        private string labelSerialNumber = "";
        private string labelManufacturer = "";
        private string labelModel = "";
        private string labelPosition = "";
        private string labelVenue = "";
        private string labelJLM = "";
        private string labelTa1 = "";
        private string labelTa2 = "";
        private string labelTa3 = "";

        private IpAdressHelper helper;
        private SQL sql;
        private Boolean isInit = true;

        public IpDevices()
        {
            InitializeComponent();
            this.Text = "IP Devices: " + Login.currentDBName; 
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new IpAdressHelper(dataGridView1, this);

            sql.sendSQL("dbo.updateIpDevices");

            fillElementsInit();

            fillTablesInit();

            initFilterSettings();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            //Manufacturer
            String[,] manufacturer = sql.receiveSQL("SELECT Manufacturer FROM vw_eng_IpDevices_Manufacturer");
            comboBox1.Items.Clear();
            comboBox1.DropDownHeight = 500;

            for (int a = 0; a < manufacturer.GetLength(1); a++)
            {
                comboBox1.Items.Add(manufacturer[0, a]);
            }
            comboBox1.Refresh();
            comboBox1.Text = "";

            //System
            String[,] system = sql.receiveSQL("SELECT Sparte FROM STAMMDATEN_systemID");
            comboBox2.Items.Clear();
            comboBox2.DropDownHeight = 500;

            for (int a = 0; a < system.GetLength(1); a++)
            {
                comboBox2.Items.Add(system[0, a]);
            }
            comboBox2.Refresh();
            comboBox2.Text = "";

            //Drucker
            comboBox3.Items.Clear();
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                comboBox3.Items.Add(pkInstalledPrinters);
            }
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                if (Login.labelPrinterName == PrinterSettings.InstalledPrinters[i])
                {
                    comboBox3.Text = Login.labelPrinterName;
                }
            }

        }

        private void initFilterSettings()
        {
            if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '{0}'", (comboBox1.Text + "*"));
        }

        private void IpDevices_Load(object sender, EventArgs e)
        {
            typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
        }
        #endregion

        #region Filter
        private void comboBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" && comboBox2.Text == "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = "";
                }
                else if (comboBox1.Text != "" && comboBox2.Text == "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '{0}'", comboBox1.Text + "*");
                }
                else if (comboBox1.Text == "" && comboBox2.Text != "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("System LIKE '{0}'", comboBox2.Text+"*");
                }
                else if (comboBox1.Text != "" && comboBox2.Text != "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '{0}' AND System LIKE '{1}'", comboBox1.Text + "*", comboBox2.Text + "*");
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        #region Export
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }
        #endregion

        #region Drucken
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count != 0)
                {

                    // Zugehöriges Label ausdrucken


                    label = new PrintDocument();
                    if (comboBox3.Text != "TEC B-EV4-T" && comboBox3.Text != "TEC B-EV4-G")
                    {
                        PaperSize pS = new PaperSize("Custom Size", 620, 620);
                        label.DefaultPageSettings.PaperSize = pS;
                        label.PrinterSettings.PrinterName = comboBox3.Text;
                        label.PrinterSettings.DefaultPageSettings.PaperSize = pS;
                        label.PrintPage += new PrintPageEventHandler(label_PrintPage);

                        //MessageBox.Show("Normal Printer; Printing with \"" + label.PrinterSettings.PrinterName + "\"\r\n\r\n"
                        //+ "PaperSize: (" + label.DefaultPageSettings.PaperSize.Width.ToString() + " x " + label.DefaultPageSettings.PaperSize.Height.ToString() + ")");

                        label.Print();
                    }
                    else
                    {
                        //Labeldrucker mit Silber-Labels
                        label.PrinterSettings.PrinterName = comboBox3.Text;
                        label.PrintPage += new PrintPageEventHandler(label_PrintPage);

                        //MessageBox.Show("Label Printer; Printing with \"" + label.PrinterSettings.PrinterName + "\"\r\n\r\n"
                        //+ "PaperSize: (" + label.DefaultPageSettings.PaperSize.Width.ToString() + " x " + label.DefaultPageSettings.PaperSize.Height.ToString() + ")");

                        label.Print();
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Fehler in button3_Click:\r\n\r\nFehlermeldung:\r\n" + ex.StackTrace.ToString());
            }
        }

        private void label_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                if (comboBox3.Text != "TEC B-EV4-T" && comboBox3.Text != "TEC B-EV4-G")
                {
                    SolidBrush brush = new SolidBrush(Color.Black);
                    System.Drawing.Font header = new System.Drawing.Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Bold);
                    Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
                    int x = 5;
                    //Image img;


                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.SelectedRows[0].Cells[i].Value.ToString() != "")
                        {
                            e.Graphics.DrawString((dataGridView1.Columns[i].HeaderText.ToString() + ": " + dataGridView1.SelectedRows[0].Cells[i].Value.ToString()), header, brush, 5, x);
                            x += 12;
                        }
                    }

                    //labelModel = dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(); 
                    //e.Graphics.DrawString("Model: " + labelModel, header, brush, 5, 5);

                    e.Graphics.DrawImage(Worktool.Properties.Resources.ASC_Logo_quadratisch, 200, 10, 30, 30);
                    //if (labelSerialNumber != null && labelSerialNumber != "")
                    //{
                    //    img = barcode128.Draw(labelSerialNumber, 30);
                    //    e.Graphics.DrawImage(img, 5, 113);
                    //}
                }
                else
                {
                    SolidBrush brush = new SolidBrush(Color.Black);
                    System.Drawing.Font header = new System.Drawing.Font(FontFamily.GenericSansSerif, 7.0F, FontStyle.Bold);
                    Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
                    int x = 5;
                    //Image img;


                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.SelectedRows[0].Cells[i].Value.ToString() != "")
                        {
                            e.Graphics.DrawString((dataGridView1.Columns[i].HeaderText.ToString() + ": " + dataGridView1.SelectedRows[0].Cells[i].Value.ToString()), header, brush, 5, x);
                            x += 12;
                        }
                    }

                    //labelModel = dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(); 
                    //e.Graphics.DrawString("Model: " + labelModel, header, brush, 5, 5);

                    e.Graphics.DrawImage(Worktool.Properties.Resources.ASC_Logo_quadratisch, 250, 0, 40, 40);
                    //if (labelSerialNumber != null && labelSerialNumber != "")
                    //{
                    //    img = barcode128.Draw(labelSerialNumber, 30);
                    //    e.Graphics.DrawImage(img, 5, 113);
                    //}
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Fehler in label_PrintPage:\r\n\r\nFehlermeldung:\r\n" + ex.StackTrace.ToString());
            }
        }
        #endregion

        #region Daten neu laden
        public void reloadAllData()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            fillElementsInit();
            fillTablesInit();
            initFilterSettings();
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }
        #endregion
        
    }
}
